package com.hspedu.newData.dmsxl.数学技巧;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-29 
 * @Version: 1.0
 */

public class LC9回文数 {


    // 转成字符串
    public boolean isPalindrome(int x) {
        String s = x+"";

        int i = 0;
        int j = s.length() - 1;

        while(i < j){
            if (s.charAt(i) != s.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    // 传统方式,只反转一半，需要注意如果是以0结尾的需要额外考虑
    // todo
    public boolean isPalindrome1(int x) {
        if ( x < 0 || (x %10 == 0 && x != 0)) return false;
        int sum = 0;
        while(x > sum){
            sum = sum*10 +  x %10;
            x = x/10;
        }
        return x == sum || x == sum /10;
    }

    @Test
    public void test() {
        // 测试代码

        boolean palindrome1 = isPalindrome1(121);
        System.out.println("-----");
    }

}
